package com.tdcy.biz.wxmgt.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.bean.menu.WxMpMenu;
import net.sf.ezmorph.bean.MorphDynaBean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tdcy.biz.wxmgt.service.WeixinService;
import com.tdcy.framework.BaseController;
import com.tdcy.framework.util.JsonUtils;
import com.tdcy.framework.util.RequestUtils;
import com.tdcy.framework.util.StringUtils;
import com.tdcy.framework.util.WebUtils;

/**
*/
@Controller
@RequestMapping(value = "wxmenu")
public class WxMenuController extends BaseController {
	@Autowired
	WeixinService wxMpService;

	@ResponseBody
	@RequestMapping(value = "/getMenu", method = RequestMethod.POST)
	public Object getMenu() throws IOException, WxErrorException {
		WxMpMenu wxMenu = wxMpService.getMenuService().menuGet();
		return WebUtils.createJSONSuccess("成功", wxMenu);
	}

	@ResponseBody
	@RequestMapping(value = "/createMenu", method = RequestMethod.POST)
	public Object createMenu() throws IOException, WxErrorException {
		String menuStr = RequestUtils.getParameter(getRequest(), "menuStr");
		List<Map<String, Object>> list = JsonUtils.toList(menuStr);
		WxMenu menu = this.transferMenu(list);
		wxMpService.getMenuService().menuCreate(menu);
		return WebUtils.createJSONSuccess("同步成功");
	}

	public WxMenu transferMenu(List<Map<String, Object>> list) {
		WxMenu menu = new WxMenu();
		List<WxMenuButton> buttons = new ArrayList<>();
		for (Map<String, Object> map : list) {
			String name = StringUtils.getString(map, "name");
			String type = StringUtils.getString(map, "type");
			String key = StringUtils.getString(map, "key");
			String mediaId = StringUtils.getString(map, "mediaId");
			String url = StringUtils.getString(map, "url");

			WxMenuButton button1 = new WxMenuButton();
			button1.setType(type);
			button1.setName(name);
			button1.setKey(key);
			button1.setMediaId(mediaId);
			button1.setUrl(url);
			if (map.get("subButtons") != null) {
				List<WxMenuButton> subButtonslist = new ArrayList<WxMenuButton>();
				List subList = (List) map.get("subButtons");
				for (int i = 0; i < subList.size(); i++) {
					MorphDynaBean bean = (MorphDynaBean) subList.get(i);

					WxMenuButton button2 = new WxMenuButton();
					if (bean.get("type") != null) {
						button2.setType((String) bean.get("type"));
					}
					if (bean.get("name") != null) {
						button2.setName((String) bean.get("name"));
					}
					if (bean.get("key") != null) {
						button2.setKey((String) bean.get("key"));
					}
					if (bean.get("mediaId") != null) {
						button2.setMediaId((String) bean.get("mediaId"));
					}
					if (bean.get("url") != null) {
						button2.setUrl((String) bean.get("url"));
					}
					subButtonslist.add(button2);
				}
				button1.setSubButtons(subButtonslist);
			}
			buttons.add(button1);
		}
		menu.setButtons(buttons);
		return menu;
	}
}
